﻿Imports SRAGWMCLib
Imports System.Drawing
Imports SRAGWMCLib.OO

Partial Public Class MapServer
    Inherits System.Web.UI.Page

#Region "Declaração de variáveis globais"

    Public AuxMap As String
    Dim MSS, dx1, dx2, dy1, dy2 As String
    Dim MagFact As Double
    Dim Pnts(5), Rng()
    Dim objMapServerHelper As MAPSRVLib.GWMMapServerHelper
    Dim legendEntries As LegendEntryCollection

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Initialize()
            LoadFeatures()
            MapDisplay()
            Cleaner()
        End If

    End Sub

    ''' <summary>
    ''' initializes the LegendEntryCollection and calls the SetMapPolygon Sub
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub Initialize()
        legendEntries = New LegendEntryCollection()
        SetMapPolygon()
    End Sub

    ''' <summary>
    ''' Contains the actions to the filling of properties, there use, and final display of the map
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub MapDisplay()
        If legendEntries.Count = 0 Then
            Return
        End If
        Dim BackgroundColor As Color = Color.White
        Dim DataBase As New DataBase("Access.GDatabase", "C:\Warehouses\USSampleData.mdb", "", "USA", "C:\Warehouses\USA.csf")
        Dim DataBase2 As New DataBase("Access.GDatabase", "C:\Warehouses\CartBase.mdb", "", _
                                      "CartBase", "C:\Warehouses\Mad_UTM_1.csf")
        Dim SRAMapServer As New SRAMapServer(0, BackgroundColor)

        GWMHelper.SetMapSettings(SRAMapServer)
        GWMHelper.Connect(DataBase2)
        GWMHelper.Connect(DataBase)


        ZoomFactor()
        MapFeatures()

        If Request.QueryString("mss") <> "" Then
            objMapServerHelper.LoadMSS(MSS)
        End If

        Dim Range As New Range(MagFact, 400000000, 10000)

        If Request.QueryString("mss") = "" Or Request.QueryString("x1") = 1 Then
            MapAtributes.Range = Rng
            MapAtributes.Width = 700
            MapAtributes.Height = 450
            GWMHelper.GenerateMap(Range)
        Else
            Dim dx As String
            Dim dy As String
            dx = CDbl(MapAtributes.Range(2)) - CDbl(MapAtributes.Range(0))
            dy = MapAtributes.Range(3) - MapAtributes.Range(1)
            MapAtributes.Range(0) = MapAtributes.Range(0) + dx * dx1
            MapAtributes.Range(1) = MapAtributes.Range(1) + dy * dy1
            MapAtributes.Range(2) = MapAtributes.Range(2) + dx * dx2
            MapAtributes.Range(3) = MapAtributes.Range(3) + dy * dy2
            GWMHelper.GenerateMap(Range)
        End If

        Dim MapServer As MAPSRVLib.GWMMapServerHelper = SRAGWMCLib.SRAMapServerBuilder.GetMap()
        AuxMap = MapServer.OutputFile
        Session("MSSFile") = MapServer.MSS()
    End Sub

    ''' <summary>
    ''' Calculates the total zoom to be done to the map
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub ZoomFactor()

        MSS = Request.QueryString("mss")

        If Request.QueryString("mss") = "" Or Request.QueryString("MagFacMultiplier") = 1 Then
            MagFact = 1
            lbl_factor.Text = "Sem Factor"
        Else
            MagFact = 1 + CDbl(Request.QueryString("MagFacMultiplier"))
            lbl_factor.Text = "Factor: " & MagFact - 1

            dx1 = Request.QueryString("x1")
            If dx1 = "" Then dx1 = 0
            dy1 = Request.QueryString("y1")
            If dy1 = "" Then dy1 = 0
            dx2 = Request.QueryString("x2")
            If dx2 = "" Then dx2 = 0
            dy2 = Request.QueryString("y2")
            If dy2 = "" Then dy2 = 0

        End If
    End Sub

    ''' <summary>
    ''' Gets the Rng to display the feature and its atributes
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub MapFeatures()
        For Each le As LegendEntry In legendEntries
            Rng = GWMHelper.DisplayFeature(le)
        Next
    End Sub

#Region "Pan Buttons"

    'Utilizar , e não .

    Protected Sub imgbtn_plus_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtn_plus.Click

        Response.Redirect("MapServer.aspx?MagFacMultiplier=0,25&x1=0,25&x2=-0,25&y1=0,25&y2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgbtn_less_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtn_less.Click

        Response.Redirect("MapServer.aspx?MagFacMultiplier=-0,25&x1=-0,25&x2=0,25&y1=-0,25&y2=0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgbtn_fit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtn_fit.Click

        Response.Redirect("MapServer.aspx?x1=1&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgnorthwest_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgnorthwest.Click

        Response.Redirect("MapServer.aspx?x1=0,25&x2=0,25&y1=-0,25&y2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgleft_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgleft.Click

        Response.Redirect("MapServer.aspx?x1=0,25&x2=0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgup.Click

        Response.Redirect("MapServer.aspx?y1=-0,25&y2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgright_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgright.Click

        Response.Redirect("MapServer.aspx?x1=-0,25&x2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgdown_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgdown.Click

        Response.Redirect("MapServer.aspx?y1=0,25&y2=0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgnortheast_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgnortheast.Click

        Response.Redirect("MapServer.aspx?x1=-0,25&x2=-0,25&y1=-0,25&y2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgsoutheast_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgsoutheast.Click

        Response.Redirect("MapServer.aspx?x1=-0,25&x2=-0,25&y1=0,25&y2=0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub imgsouthwest_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgsouthwest.Click

        Response.Redirect("MapServer.aspx?x1=0,25&x2=0,25&y1=0,25&y2=0,25&mss=" & Session("MSSFile"))

    End Sub

#End Region

    ''' <summary>
    ''' Loads the features to display on the map
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub LoadFeatures()

        Dim Feature(3) As String
        Dim Tooltip As String = String.Empty
        Dim NumPairs As Double
        Dim FillColor As Color = Color.White
        Dim BorderColor As Color = Color.Black
        Dim SymbologyWeight, SymbologyStyle, SpatialOperator As Integer
        Dim DBName, TableName As String

        SymbologyStyle = 0
        SymbologyWeight = 1
        DBName = "USA"
        SpatialOperator = 4

        NumPairs = (Pnts.Length / 2)
        If Me.cb_Rios.Checked And Not LegendEntriesContains("Marker Rivers") Then
            TableName = "Rivers"
            Dim Atributes() As String = {"RIVER_NAME"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )
        End If

        If Me.cb_Estradas.Checked And Not LegendEntriesContains("Marker Interstates") Then
            TableName = "Interstates"
            Dim Atributes() As String = {"ROUTE_NUMBER"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                                    FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )

        End If

        If Me.cb_Cidades.Checked And Not LegendEntriesContains("Marker Cities") Then
            TableName = "Cities"
            Dim Atributes() As String = {"CITY_NAME"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                        FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )
        End If

        If Me.cb_Portagens.Checked And Not LegendEntriesContains("Marker Freguesias") Then
            TableName = "Freguesias_old"
            DBName = "CartBase"
            Dim Atributes() As String = {"Freg_Des"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                    FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )
        End If

        If Me.dd_Layout.SelectedItem.Text = "Condados" Then
            TableName = "Counties"
            Dim Atributes() As String = {"COUNTY_NAME"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                    FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )

        ElseIf Me.dd_Layout.SelectedItem.Text = "Estados" Then
            TableName = "States"
            Dim Atributes() As String = {"STATE_NAME"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                    FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )
        Else
            TableName = "States"
            Dim Atributes() As String = {"STATE_NAME"}
            Dim MarkerName As String = "Marker" + TableName
            legendEntries.Add( _
                GWMHelper.LegendEntryBuilder(Atributes, Tooltip, SymbologyStyle, SymbologyWeight, BorderColor, _
                    FillColor, DBName, TableName, Pnts, NumPairs, SpatialOperator, MarkerName) _
            )


        End If
    End Sub


#Region "Auxiliar Functions"

    ''' <summary>
    ''' Define the boundary of the map
    ''' </summary>
    ''' <remarks></remarks>

    Sub SetMapPolygon()
        Pnts(0) = -315432.33
        Pnts(1) = 8909.76
        Pnts(2) = 104033.34
        Pnts(3) = -837648.24
        Pnts(4) = 485365.78
        Pnts(5) = -219889.71
    End Sub

    ''' <summary>
    ''' Returns true if the legend entry is already in the collection
    ''' </summary>
    ''' <param name="legendName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' 

    Function LegendEntriesContains(ByVal legendName As String) As Boolean
        For Each legend In legendEntries
            If legend.DataSource.DBName = legendName Then
                Return True
            End If
        Next
        Return False
    End Function

#End Region

    ''' <summary>
    ''' Generates map depending on the checked checkbox
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>

    Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cb_Rios.CheckedChanged, cb_Estradas.CheckedChanged, cb_Cidades.CheckedChanged, cb_Portagens.CheckedChanged
        Initialize()
        LoadFeatures()
        MapDisplay()
        Cleaner()
    End Sub

    ''' <summary>
    ''' Cleans the .COM objects
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Sub Cleaner()


    End Sub

    Protected Sub dd_Layout_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_Layout.SelectedIndexChanged
        Initialize()
        LoadFeatures()
        MapDisplay()
        Cleaner()
    End Sub
End Class